const {
  getCoachList,
  getCoachDetailById,
  getCoachDetailByUsername,
  updateCoach,
  getCoachNameList, delCoach, newCoach, updateCoachReverseCar, cancelReverseCar
} = require('../controller/coach')
const {getCarDetail, updateCar, cancelUpdateCar} = require("../controller/car");
const {getStudentInformation} = require("../controller/student");
const {newAccount, deleteAccountByUsername} = require("../controller/user");
const {getArrangeByUsername} = require("../controller/arrange");
const {getReverseListByCoachId} = require("../controller/reverse");
const Response = require('../common/Response')
const router = require('koa-router')()

router.prefix('/api/coach')

// 获取所有教练
router.post('/list', async (context, next) => {
  const rows = await getCoachList()
  context.body = Response.success(rows)
})
// 添加教练，创建默认账户
router.post('/new', async (context, next) => {
  const CoachValue = await newCoach(context.request.body.coach)
  if (CoachValue) {
    const UserValue = await newAccount(context.request.body.account)
    if (UserValue) {
      context.body = Response.success('添加学员成功')
    }
  } else {
    context.body = Response.failure('添加学员失败')
  }
})
// 更新教练 phone,progress
router.post('/update', async (context, next) => {
  const val = await updateCoach(context.request.body)
  if (val) {
    context.body = Response.success('修改教练信息成功')
  } else {
    context.body = Response.failure('修改教练信息失败')
  }
})
// 删除教练，同时删除账户
router.post('/del/:id', async (context, next) => {
  const CoachValue = await delCoach(context.params.id)
  if (CoachValue) {
    const UserValue = await deleteAccountByUsername(context.request.body.identify)
    if (UserValue) {
      context.body = Response.success('删除学员成功')
    }
  } else {
    context.body = Response.failure('删除学员失败')
  }
})

// 根据 username 查询教练信息 -- 教练操作
router.post('/detail/username', async (context, next) => {
  const row = await getCoachDetailByUsername(context.session.username)
  if (row.identify) {
    context.body = Response.success(row)
  } else {
    context.body = Response.failure('获取教练详细信息失败')
  }
})
// 获取教练预约车辆信息
router.get('/detail/car', async (context, next) => {
  // 获取教练预约车辆信息
  const CoachInfo = await getCoachDetailByUsername(context.session.username)
  if (CoachInfo.reverse_car) {
    const CarInfo = await getCarDetail(CoachInfo.reverse_car)
    if (CarInfo.car_number) {
      context.body = Response.success(CarInfo)
    } else {
      context.body = Response.failure()
    }
  } else {
    context.body = Response.failure()
  }
})
// 教练预约车辆
router.post('/reverse', async (context, next) => {
  const val = await updateCoachReverseCar(context.session.username, context.request.body.car_number)
  if (val) {
    const CarVal = await updateCar(context.request.body)
    if (CarVal) {
      context.body = Response.success('预约车辆成功')
    }
  } else {
    context.body = Response.failure('预约车辆失败')
  }
})
// 教练取消预约车辆
router.post('/reverse/cancel', async (context, next) => {
  const val = await cancelReverseCar(context.session.username)
  if (val) {
    const CarVal = await cancelUpdateCar(context.request.body)
    if (CarVal) {
      context.body = Response.success('取消预约车辆成功')
    }
  } else {
    context.body = Response.failure('取消预约车辆失败')
  }
})
// 获取教练练车时间安排
router.post('/arrange/username', async (context, next) => {
  const CoachInfo = await getCoachDetailByUsername(context.session.username)
  const ArrangeInfo = await getArrangeByUsername(context.session.username)
  context.body = Response.success({
    identify: CoachInfo.identify,
    name: CoachInfo.name,
    isArranged: CoachInfo.isArranged,
    ...ArrangeInfo
  })
})

// 根据学员 identify 获取教练时间安排
router.post('/arrange', async (context, next) => {
  // 1. 获取学员信息
  const StudentInfo = await getStudentInformation(context.session.username)
  // 2. 获取教练信息
  const CoachInfo = await getCoachDetailById(StudentInfo.coach_id)
  // 3. 获取练车时间安排
  const ArrangeInfo = await getArrangeByUsername(CoachInfo.identify)
  context.body = Response.success({
    coach_id: CoachInfo.id,
    ...ArrangeInfo
  })
})
// 根据学员的 coach_id 查询教练信息 -- 学员操作
router.post('/detail', async (context, next) => {
  const StudentInfo = await getStudentInformation(context.session.username)
  if (StudentInfo.identify) {
    const row = await getCoachDetailById(StudentInfo.coach_id)
    if (row.identify) {
      context.body = Response.success(row)
    } else {
      context.body = Response.failure('获取教练详细信息失败')
    }
  } else {
    context.body = Response.failure('获取教练详细信息失败')
  }
})
// 获取教练 name,id --> 供学员选择
router.get('/list/name', async (context, next) => {
  const rows = await getCoachNameList()
  context.body = Response.success(rows)
})
// 获取预约情况
router.get('/list/reverse', async (context, next) => {
  const CoachInfo = await getCoachDetailByUsername(context.session.username)
  const ArrangeInfo = await getArrangeByUsername(context.session.username)
  const Students = await getReverseListByCoachId(CoachInfo.id)
  context.body = Response.success({
    number_limit: ArrangeInfo.number_limit,
    data: Students
  })
})

module.exports = router;
